<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class users extends MY_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->lang->load('user');
        //$this->load->model('menu_model');
    }
    
    public function index()
    {
        $this->load->model('user_model');
        if( !has_permission('System.User.PageView') ){
            redirect('no_access');
        }
        //$this->jimauth->hash_password('asasa');
        
        $this->template->set_page_title( lang('user_title_user') );
        $this->template->set_view('users/content_home');
        
        $this->template->render();
    }
    
    public function datatable_users(){
        $this->load->model('user_model');
        $aaData = $this->user_model->load_datatable_users($_GET,1);
        $totalData = $this->user_model->load_datatable_users($_GET,2);
        
        $output = array(
            "sEcho" => intval($_GET['sEcho']),
            "iTotalRecords" => $aaData['total'],
            "iTotalDisplayRecords" => $totalData['total'],//$iFilteredTotal,
            "aaData" => $aaData['aaData']
        );
    
        echo json_encode( $output );
    }
    
    public function ajax_loadfrm_user($id = null){
        $this->load->model('user_model');
        $this->load->model('roles_model');
        $data = array();
        
        $data['roles'] = $this->roles_model->getRoleActive();
        //$data['category'] = $this->vt_model->getAllCategory();
        
        if($id != null){
            $data['user'] = $this->user_model->getUserById($id);
            $data['active'] = site_url('users/edit');
            $data['act'] = 'edit';
        }else{
            $data['active'] = site_url('users/add');
            $data['act'] = 'add';
        }
        
        $this->load->view($this->template->template.'/users/form_add_user',$data);
    }
    
    public function add(){
        $this->load->model('user_model');
        $this->load->helper('email');
        if( !has_permission('System.User.Create') ){
            $data = array(
                'flag' => false,
                'error' => array('message' => 'No accsess!','name' => 'error')
            );
            echo json_encode($data);
            exit;
        }
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $username = trim( $this->input->post('username') );
            $full_name = trim( $this->input->post('full_name') );
            $password = trim( $this->input->post('password') );
            $repassword = $this->input->post('repassword');
            $email = trim( $this->input->post('email') );
            $role_id = $this->input->post('role_id');
            $status = $this->input->post('status');
            
            $flag = true;
            $id = 0;
            $error = array();
            if( $username == '' ){
                $flag = false;
                $error[] = array(
                    'message' => lang('user_error_01'),
                    'name' => 'username'
                );
            }else{
                if( $this->user_model->unique_user($username) == false ){
                    $flag = false;
                    $error[] = array(
                        'message' => lang('user_error_02'),
                        'name' => 'username'
                    );
                }
            }
            if( $full_name == '' ){
                $flag = false;
                $error[] = array(
                    'message' => lang('user_error_09'),
                    'name' => 'full_name'
                );
            }
            if($password == ''){
                $flag = false;
                $error[] = array(
                    'message' => lang('user_error_03'),
                    'name' => 'password'
                );
            }else{
                if($password != $repassword){
                    $flag = false;
                    $error[] = array(
                        'message' => lang('user_error_04'),
                        'name' => 'password'
                    );
                }
            }
            
            if($email == ''){
                $flag = false;
                $error[] = array(
                    'message' => lang('user_error_05'),
                    'name' => 'email'
                );
            }else{
                if (!valid_email($email)){
                    $flag = false;
                    $error[] = array(
                        'message' => lang('user_error_06'),
                        'name' => 'email'
                    );
                }
            }
            
            if($role_id == false){
                $flag = false;
                $error[] = array(
                    'message' => lang('user_error_07'),
                    'name' => 'role_id'
                );
            }
            
            if($flag == true){
                $salt = substr(md5(time()), 5, 10);
                
                $password_hash = $this->jimauth->hash_password($password.$salt);
                
                $data_insert = array(
                    'username' => $username,
                    'full_name' => $full_name,
                    'password_hash' => $password_hash,
                    'salt' => $salt,
                    'email' => $email,
                    'role_id' => $role_id,
                    'created_date' => date('Y-m-d H:i:s'),
                    'last_login' => null,
                    'status' => $status
                );
                
                $id = $this->user_model->insertUser($data_insert);
            }
            
            $data = array(
                'flag' => $flag,
                'error' => $error,
                'id' => $id
            );
            echo json_encode($data);
            
        }
    }
    public function edit(){
        $this->load->model('user_model');
        $this->load->helper('email');
        if( !has_permission('System.User.Update') ){
            $data = array(
                'flag' => false,
                'error' => array('message' => 'No accsess!','name' => 'error')
            );
            echo json_encode($data);
            exit;
        }
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $id = $this->input->post('id');
            $username = trim( $this->input->post('username') );
            $full_name = trim( $this->input->post('full_name') );
            $password = trim( $this->input->post('password') );
            $repassword = $this->input->post('repassword');
            $email = trim( $this->input->post('email') );
            $role_id = $this->input->post('role_id');
            $status = $this->input->post('status');
            
            $flag = true;
            $error = array();
            if( $username == '' ){
                $flag = false;
                $error[] = array(
                    'message' => lang('user_error_01'),
                    'name' => 'username'
                );
            }else{
                if( $this->user_model->unique_user_edit($id, $username) == false ){
                    $flag = false;
                    $error[] = array(
                        'message' => lang('user_error_02'),
                        'name' => 'username'
                    );
                }
            }
            
            if( $full_name == '' ){
                $flag = false;
                $error[] = array(
                    'message' => lang('user_error_09'),
                    'name' => 'full_name'
                );
            }
            
            if($password != ''){
                if($password != $repassword){
                    $flag = false;
                    $error[] = array(
                        'message' => lang('user_error_04'),
                        'name' => 'password'
                    );
                }
            }
            
            if($email == ''){
                $flag = false;
                $error[] = array(
                    'message' => lang('user_error_05'),
                    'name' => 'email'
                );
            }else{
                if (!valid_email($email)){
                    $flag = false;
                    $error[] = array(
                        'message' => lang('user_error_06'),
                        'name' => 'email'
                    );
                }
            }
            
            if($role_id == false){
                $flag = false;
                $error[] = array(
                    'message' => lang('user_error_07'),
                    'name' => 'role_id'
                );
            }
            
            $old_user = $this->user_model->getUserById($id);
            
            if($flag == true){
                $data_update = array(
                    'username' => $username,
                    'full_name' => $full_name,
                    'email' => $email,
                    'role_id' => $role_id,
                    'status' => $status
                );
                if($password != ''){
                    $data_update['password_hash'] = $this->jimauth->hash_password($password.$old_user->salt);
                }
                
                $this->user_model->updateUser($data_update,$id);
            }
            $user = $this->user_model->getUserById($id);
            $html = '';
            if($user->status == -1){
                $html = '<img src="'.base_url('template/ess/images/trash.png').'" alt="">';
            }elseif($user->status == 0){
                $html = '<img src="'.base_url('template/ess/images/icon-16-notice-note.png').'" alt="">';
            }elseif($user->status == 1){
                $html = '<img src="'.base_url('template/ess/images/tick.png').'" alt="">';
            }elseif($user->status == 2){
                $html = '<img src="'.base_url('template/ess/images/publish_x.png').'" alt="">';
            }
            $data = array(
                'flag' => $flag,
                'error' => $error,
                'id' => $id,
                'username' => $user->username,
                'full_name' => $user->full_name,
                'role' => $user->role_name,
                'email' => $user->email,
                'status' => $html
            );
            echo json_encode($data);
            
        }
    }
    public function delete(){
        $this->load->model('user_model');
        if( !has_permission('System.User.Delete') ){
            $data = array(
                'flag' => false,
                'error' => array('message' => 'No accsess!','name' => 'error')
            );
            echo json_encode($data);
            exit;
        }
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $id = $this->input->post('id');
            $flag = true;
            $message = '';
            $user = $this->user_model->getUserById($id);
            if($user->can_delete == 1){
                $flag = false;
                $message = lang('user_error_08');
            }else{
                $data_update = array('status' => -1);
                $this->user_model->updateUser($data_update,$id);
            }
            
            $data = array(
                'flag' => $flag,
                'message' => $message,
            );
            echo json_encode($data);
        }
    }

}